package sparrowGui.components
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import sparrowGui.components.item.ItemFactory;
	import sparrowGui.components.item.SListItem;
	import sparrowGui.components.item.SRichButton;
	import sparrowGui.event.ListEvent;
	import sparrowGui.i.IItemFactory;
	
	/**
	 * 组合下拉控件,点击会弹出菜单按钮
	 * 
	 * 例子如下
	 * 
		var cbx:SCombobox = new SCombobox(this);
		cbx.update([11,22,33]);
		addChild(cbx);
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SCombobox extends BaseUIComponent
	{
		private var _popTip:SPopupMenu;
		private var _comboItem:SRichButton;
		
		private var _data:Array;
		
		/**
		 * 构造组合下拉控件
		 * @param tipParent		弹出的下拉列表的父容器
		 * @param uiVars		皮肤变量
		 * @param cellFactory 子项工厂，用于创建子项和子项皮肤，工厂里还有缓存的功能
		 * 
		 */
		public function SCombobox(showParent:DisplayObjectContainer=null
								  ,uiVars:Object=null,itemFactory:IItemFactory=null)
		{
			super(uiVars);
			
			itemFactory = itemFactory || new ItemFactory(SListItem,"comboboxItem");
			_popTip = new SPopupMenu(showParent,uiVars,itemFactory);
			popTip.showPos = BaseTip.TIPS_POSI_DM;
			popTip.list.mustSelect = true;
			
			_comboItem = new SRichButton(skin);
			
			addSkinListen();
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
			super.setUI(uiVars);
			_popTip
		}
		
		/**
		 * 更新内容
		 */
		public function update(o:Array):void
		{
			if(o && o.length)
				comboItem.update(o[0]);
			else
				comboItem.update(null);
			
			_popTip.update(o);
			
			_data = o;
		}
		
		override protected function addSkinListen():void
		{
			super.addSkinListen();
			skin.addEventListener(MouseEvent.CLICK,onMouseEvent);
			popTip.list.addEventListener(ListEvent.LIST_ITEM_SELECT,onSelectEvt);
			popTip.addEventListener(Event.CANCEL,onCancel);
		}
		
		override protected function removeSkinListen():void
		{
			super.removeSkinListen();
			skin.removeEventListener(MouseEvent.CLICK,onMouseEvent);
			popTip.removeEventListener(Event.CANCEL,onCancel);
			popTip.list.removeEventListener(ListEvent.LIST_ITEM_SELECT,onSelectEvt);
		}
		
		private function onCancel(e:Event):void
		{
			comboItem.selected = false;
//			popTip.clearUp();
		}
		
		private function onSelectEvt(e:ListEvent):void
		{
			comboItem.update(e.itemData);
			dispatchEvent(e);
		}
		
		/**
		 * 监听鼠标事件
		 * @param e
		 */
		private function onMouseEvent(e:MouseEvent):void
		{
			if(comboItem.selected)
				popTip.showToTarget(skin);
			else
				popTip.clearUp();
			
/*			if(comboItem.selected)
				popTip.show(_data,skin);
			else
				popTip.clearUp();*/
		}

		/**
		 * 信息
		 */
		public function get data():Array
		{
			return _data;
		}

		/**
		 * 弹出列表提示组件
		 */
		public function get popTip():SPopupMenu
		{
			return _popTip;
		}

		/**
		 * 组件单项
		 */
		public function get comboItem():SRichButton
		{
			return _comboItem;
		}
		
		override public function getDefaultUIName():String
		{
			return "combobox";
		}
	}
}